/**
 * MenuFrameLoadGame.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2011 Roslan Amir. All rights reserved.
 */

package com.roslan.games.moo3d.ui.menu;

import java.io.File;
import java.io.FilenameFilter;
import java.sql.Timestamp;

import com.ardor3d.extension.ui.UIButton;
import com.ardor3d.extension.ui.UILabel;
import com.ardor3d.extension.ui.UIPanel;
import com.ardor3d.extension.ui.UIRadioButton;
import com.ardor3d.extension.ui.event.ActionEvent;
import com.ardor3d.extension.ui.event.ActionListener;
import com.ardor3d.extension.ui.layout.GridLayout;
import com.ardor3d.extension.ui.layout.GridLayoutData;
import com.ardor3d.extension.ui.layout.RowLayout;
import com.ardor3d.extension.ui.util.ButtonGroup;
import com.roslan.games.moo3d.GameData.Actions;

/**
 * Comments go here.
 * 
 * @author Roslan Amir
 * @version 1.0 - Jan 27, 2011
 */
public class MenuFrameLoadGame extends MenuFrameBase {

	/**
	 * Default no-arg constructor.
	 * 
	 * @param camera
	 */
	public MenuFrameLoadGame() {
		super("Load Game");

		/*
		 * Panel with a Grid layout
		 */
		final UIPanel grid = new UIPanel();
		grid.setLayout(new GridLayout());
		grid.setMargin(FRAME_MARGINS);
		grid.setBorder(SOLID_BORDER);
		vpanel.add(grid);

		/*
		 * Radio buttons for each save file: name + date + time
		 */
		final ButtonGroup saveFileGroup = new ButtonGroup();

		/*
		 * Create the radio buttons and associated labels
		 */
		for (int row = 0; row < 6; row++) {
			final UIRadioButton rb = new UIRadioButton("[b]" + (row + 1) + ".[/b]");
			rb.setStyledText(true);
			rb.setSelectable(true);
			rb.setSelected(gameData.saveFileIndex == row);
			rb.setGroup(saveFileGroup);
			grid.add(rb);

			final UILabel nameLabel = new UILabel("");
			nameLabel.setStyledText(true);
			grid.add(nameLabel);

			final UILabel dateLabel = new UILabel("");
			dateLabel.setStyledText(true);
			grid.add(dateLabel);

			final UILabel timeLabel = new UILabel("");
			timeLabel.setStyledText(true);
			timeLabel.setLayoutData(GridLayoutData.WrapAndGrow);
			grid.add(timeLabel);

			rb.addActionListener(new RadioButtonListener(row, nameLabel));
		}

		/*
		 * Panel with a horizontal layout
		 */
		final UIPanel hpanel = new UIPanel();
		hpanel.setLayout(new RowLayout(true));
		hpanel.setBorder(SOLID_BORDER);
		vpanel.add(hpanel);

		/*
		 * The Back button
		 */
		final UIButton backButton = new UIButton("[b]Back[/b]");
		backButton.setStyledText(true);
		// backButton.setGap(0);
		// backButton.setMargin(MARGINS);
		// backButton.setPadding(PADDING);
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_MAIN_MENU;
			}
		});
		hpanel.add(backButton);

		/*
		 * The Continue button
		 */
		final UIButton continueButton = new UIButton("[b]Continue[/b]");
		continueButton.setStyledText(true);
		// continueButton.setGap(0);
		// continueButton.setMargin(MARGINS);
		// continueButton.setPadding(PADDING);
		continueButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.CONTINUE_GAME;
			}
		});
		hpanel.add(continueButton);

		/*
		 * Finalization
		 */
		this.setName("Load Game");
		this.setContentPanel(vpanel);
		super.setUpFrame();
	}

	/**
	 * Update the contents of this frame before showing it.
	 */
	public void update() {
		File saveDir = new File("save/");
		logger.info("Current directory = " + saveDir.getAbsolutePath());
		String[] list = saveDir.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(".save");
			}
		});
		logger.info("Number of SAVE files = " + list.length);

		UIPanel vpanel = this.getContentPanel();
		UIPanel grid = (UIPanel) vpanel.getChild(0);

		int row = 0;
		for (String filename : list) {
			UIRadioButton radiobutton = (UIRadioButton) grid.getChild(row * 4);
			radiobutton.setText("[b]" + filename + "[/b]");
			radiobutton.setSelected(row == 0);
			File file = new File(saveDir, filename);
			long ts = file.lastModified();
			Timestamp timestamp = new Timestamp(ts);
			String str = timestamp.toString();
			UILabel nameLabel = (UILabel) grid.getChild(row * 4 + 1);
			nameLabel.setText("[b]" + filename + "[/b]");
			UILabel dateLabel = (UILabel) grid.getChild(row * 4 + 2);
			dateLabel.setText("[b]" + str.substring(0, 10) + "[/b]");
			UILabel timeLabel = (UILabel) grid.getChild(row * 4 + 3);
			timeLabel.setText("[b]" + str.substring(11, 19) + "[/b]");
			row++;
		}

		while (row < 6) {
			UIRadioButton radiobutton = (UIRadioButton) grid.getChild(row * 4);
			radiobutton.setText("");
			radiobutton.setEnabled(false);
			UILabel nameLabel = (UILabel) grid.getChild(row * 4 + 1);
			nameLabel.setText("");
			nameLabel.setVisible(false);
			UILabel dateLabel = (UILabel) grid.getChild(row * 4 + 2);
			dateLabel.setText("");
			dateLabel.setVisible(false);
			UILabel timeLabel = (UILabel) grid.getChild(row * 4 + 3);
			timeLabel.setText("");
			timeLabel.setVisible(false);
			row++;
		}

		UIPanel hpanel = (UIPanel) vpanel.getChild(1);
		UIButton continueButton = (UIButton) hpanel.getChild(1);
		continueButton.setEnabled(list.length != 0);

		this.layout();
		this.pack();
	}

	/**
	 * Comments go here.
	 */
	private class RadioButtonListener implements ActionListener {
		private final int row;
		private final UILabel nameLabel;

		private RadioButtonListener(int row, UILabel nameLabel) {
			this.row = row;
			this.nameLabel = nameLabel;
		}

		public void actionPerformed(ActionEvent event) {
			gameData.saveFileIndex = row;
			gameData.saveFileName = nameLabel.getText();
		}
	}

}
